package org.eggdo.apof.server;

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;

import com.sun.image.codec.jpeg.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: Draw
 * 
 */
public class Draw extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Draw() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("+++++++++++");
		System.out.println("What the fuck is true?");
		response.setContentType("image/jpeg");
		createImage(response.getOutputStream());
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private void createImage(OutputStream out) {
		XmlParser xp = new XmlParser("Apof");
		
		int width = 212;
		int height = 547;
		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bi.createGraphics();
		g.setBackground(Color.white);

		g.setColor(new Color(0xFFFFFF));
		g.fillRect(0, 0, width, height); 
	
		Font contentFont = new Font("", Font.PLAIN, 12);
		Font timeFont =  new Font("",Font.PLAIN,9);
		

		g.setColor(Color.blue);
		g.drawRect(0,0,width-3,height-3); 

		g.setColor(Color.gray);
		g.setFont(new Font("",Font.BOLD,30));
		g.drawString(xp.getTitle(), 80, 40);
		ArrayList<Item> items = xp.getItems();
		
		int y = 80;
		try {
			
			for (Iterator<Item> itr = items.iterator(); itr.hasNext();) {
				Item nd = (Item) itr.next();
				
				g.setColor(Color.blue);
				g.setFont(contentFont);
				g.drawString(nd.getTitle(), 5, y);
				
				g.setColor(Color.cyan);
				g.setFont(timeFont);
				g.drawString(nd.getPubDate(),5 , y+15);
				
				y += 32;
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		try {
		FileInputStream logoFile = new FileInputStream("./logo.gif");
		InputStream headFile = xp.getImage();
		Image logo=javax.imageio.ImageIO.read(logoFile);
		Image head=javax.imageio.ImageIO.read(headFile);
		int w = logo.getWidth(null);
		int h = logo.getHeight(null);
		g.drawImage(head, 10 , 10, head.getWidth(null),head.getHeight(null),null);
		g.drawImage(logo, 3, 450, w, h ,null); 
		
		

		// end draw:
		g.dispose();
		bi.flush();
		// encode:
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
		param.setQuality(1.0f, false);
		encoder.setJPEGEncodeParam(param);
		
			encoder.encode(bi);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

}